package com.pe1n.yangtaopai.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.jude.rollviewpager.OnItemClickListener;
import com.jude.rollviewpager.RollPagerView;
import com.pe1n.yangtaopai.R;
import com.pe1n.yangtaopai.bean.ListBean;
import com.pe1n.yangtaopai.bean.ListfourBean;
import com.pe1n.yangtaopai.bean.ListthreeBean;
import com.pe1n.yangtaopai.bean.ListtwoBean;
import com.pe1n.yangtaopai.view.activity.ItemOneListActivity;

import java.util.ArrayList;
import java.util.List;
//大适配器
/**
 * Created by Administrator on 2016/11/22.
 */

public class MyAmountRecylerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context mContext;
    private List<Object> myBeanList;

    private static final int TYPE1 = 0, TYPE2 = 1, TYPE3 = 2, TYPE4 = 3;

    public MyAmountRecylerAdapter(Context mContext, List<Object> myBeanList) {
        this.mContext = mContext;
        this.myBeanList = myBeanList;

    }

    @Override
    public int getItemCount() {
        return myBeanList.size();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE1:
                View view1 = LayoutInflater.from(mContext).inflate(R.layout.amount_fragment_bean_one_layout, parent, false);
                return new MyViewHolder1(view1);
            case TYPE2:
                View view2 = LayoutInflater.from(mContext).inflate(R.layout.amount_fragment_bean_two_layout, parent, false);
                return new MyViewHolder2(view2);

            case TYPE3:
                View view3 = LayoutInflater.from(mContext).inflate(R.layout.amount_fragment_bean_three_layout, parent, false);
                return new MyViewHolder3(view3);

            case TYPE4:
                View view4=LayoutInflater.from(mContext).inflate(R.layout.amount_fragment_bena_four_layout,parent,false);
                return new MyViewHolder4(view4);
        }
        return null;

    }

    //根据对应的子布局添加数据
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof MyViewHolder1) {
            List<ListBean.DataBean> listOneBean=((ListBean)myBeanList.get(position)).getData();
            ((MyViewHolder1)holder).restaData(listOneBean);

            //这是记载一长图片的方法
          /*  ListBean.DataBean dataBean = ((ListBean) myBeanList.get(position)).getData().get(0);
            String string = dataBean.getImagePath();
            if (string != null) {
                Glide.with(mContext).load(string)
                        .placeholder(R.drawable.place_holder)
                        .into(((MyViewHolder1) holder).imageView);
            }*/

        } else if (holder instanceof MyViewHolder2) {
            List<ListtwoBean.DataBean> listTwoBean = ((ListtwoBean) myBeanList.get(position)).getData();
            ((MyViewHolder2) holder).resetData(listTwoBean);


        } else if (holder instanceof MyViewHolder3) {
            List<ListthreeBean.DataBean> listThreeBean = ((ListthreeBean) myBeanList.get(position)).getData();
            ((MyViewHolder3) holder).resetData3(listThreeBean);

        }else if (holder instanceof MyViewHolder4){
            List<ListfourBean.DataBean> listFourBean = ((ListfourBean)myBeanList.get(position)).getData();
            ((MyViewHolder4) holder).resetData4(listFourBean);

        }
    }

    @Override
    public int getItemViewType(int position) {
        if(myBeanList.get(position) instanceof ListBean){
            return TYPE1;
        }
        else if(myBeanList.get(position) instanceof ListtwoBean){
            return TYPE2;
        }else if(myBeanList.get(position) instanceof ListthreeBean){
            return TYPE3;
        }else if (myBeanList.get(position) instanceof ListfourBean){
            return TYPE4;
        }

        return super.getItemViewType(position);
    }


    class MyViewHolder1 extends RecyclerView.ViewHolder {
        private RollPagerView rollPagerView;
        private List<ListBean.DataBean>listOnebean=new ArrayList<>();
        public void restaData(List<ListBean.DataBean>a){
            listOnebean.clear();
            listOnebean.addAll(a);
            rollPagerView.getViewPager().getAdapter().notifyDataSetChanged();
            rollPagerView.setOnItemClickListener(new OnItemClickListener() {
                //点击监听
                @Override
                public void onItemClick(int position) {
                    Intent intent1=new Intent();
                    intent1.setClass(mContext,ItemOneListActivity.class);
                    intent1.putExtra("OneImgPath",listOnebean.get(position).getImagePath());
                    intent1.putExtra("OneImgTitle",listOnebean.get(position).getTitle());

                    intent1.putExtra("ID",listOnebean.get(position).getTargetId());
                    mContext.startActivity(intent1);
                }
            });
        }
        ImageView imageView;

        public MyViewHolder1(View itemView){
            super(itemView);
            rollPagerView= (RollPagerView) itemView.findViewById(R.id.amount_fragment_rollpagerview);
            rollPagerView.setAdapter(new MyAmountItemOneRollViewPagerAdapter(rollPagerView,mContext,listOnebean));


        }
    }

    class MyViewHolder2 extends RecyclerView.ViewHolder {
        XRecyclerView recyclerView;
        List<ListtwoBean.DataBean> listTwoBean = new ArrayList<>();

        public void resetData(List<ListtwoBean.DataBean> l) {
            listTwoBean.clear();
            listTwoBean.addAll(l);
            recyclerView.getAdapter().notifyDataSetChanged();


        }

        public MyViewHolder2(View itemView) {
            super(itemView);
            recyclerView = (XRecyclerView) itemView.findViewById(R.id.amount_fragment_beanTwo_layout_Xr);
            recyclerView.setLayoutManager(new GridLayoutManager(mContext, 4 ,OrientationHelper.VERTICAL ,false));
            recyclerView.setAdapter(new MyAmountItemRecyclerAdapter(mContext, listTwoBean));
        }
    }

    class MyViewHolder3 extends RecyclerView.ViewHolder {
        XRecyclerView recyclerView3;
        List<ListthreeBean.DataBean>listThreeBean=new ArrayList<>();

        public void resetData3(List<ListthreeBean.DataBean>a){
            listThreeBean.clear();
            listThreeBean.addAll(a);
            recyclerView3.getAdapter().notifyDataSetChanged();
        }


        public MyViewHolder3(View itemView) {
            super(itemView);
            recyclerView3 = (XRecyclerView) itemView.findViewById(R.id.amount_fragment_beanThree_layout_Xr);
            recyclerView3.setLayoutManager(new GridLayoutManager(mContext, 2, OrientationHelper.VERTICAL, false));
            recyclerView3.setAdapter(new MyAmountItemThreeRecyclerAdapter(mContext, listThreeBean));

        }
    }

    class MyViewHolder4 extends RecyclerView.ViewHolder {
        RecyclerView recyclerView4;
        List<ListfourBean.DataBean>listFourBean=new ArrayList<>();
        public void resetData4(List<ListfourBean.DataBean>d){
            listFourBean.clear();
            listFourBean.addAll(d);
            recyclerView4.getAdapter().notifyDataSetChanged();
        }

        public MyViewHolder4(View itemView) {
            super(itemView);
            recyclerView4= (RecyclerView) itemView.findViewById(R.id.item_listfour_recyclerview);
            recyclerView4.setLayoutManager(new GridLayoutManager(mContext,2,OrientationHelper.VERTICAL,false));
            recyclerView4.setAdapter(new MyAmuontItemFourAdapter(mContext,listFourBean));


        }
    }
}
